clear
set more off
macro drop all
capture log close

/********************************************************************************
Discrimination in Multi-Phase Systems: Evidence from Child Protection

Created on: 8/29/18

Last Modified on: 2/20/2024

Description: This file cleans the living arrangements data

Note that we have removed the file directory names from this program for 
confidentiality reasons.
********************************************************************************/

** Setting the Directory
global rawdata 
global cleandata 
global tmp 

/********************************************************************************

This file should be clean at the child*case*removal date level.

There are 2 sections of this do file:
1) Clean Variables
2) Collapse to Child Level

*******************************************************************************/

*************************
**1) CLEAN VARIABLES
*************************

**Load raw living arrangements data
use "${rawdata}living_arrangements.dta", clear

**Rename variables
rename childpartyid vicid
rename pcmnt_episode placementid
gen startdt=date(provbegindt, "YMD")
gen enddt=date(provenddate, "YMD")
drop provbegin provend
rename livingarrangedesc placement_type
keep vicid placementid start end placement_type
sort vicid placementid startdt

**Keep only the first and the last day that they are in foster care
bysort vicid placementid: egen fc_startdt=min(startdt)
bysort vicid placementid: egen fc_enddt=max(enddt)

**Keep last placement date only if it ended in a terminal outcome. Otherwise, child is still in FC.
tab placement_type
gen terminal=0
foreach x in "Adoptive Home" "College Dormitory" "Independent Living" "Juvenile Guardianship Home" ///
	"Out of State Parental Home" "Parental Home" "Pre-Adoptive Home" "Rental Home/Apartment" ///
	"EPIC Guardianship Home" {
	replace terminal=1 if placement_type=="`x'"
}
replace fc_enddt=. if terminal==0

**Now identify a child's FIRST foster placement type. Categorize unclear placements as unrelated. 
gen type_kin=0
gen type_unrel=0
gen type_group_inst=0
foreach x in "Out of State Licensed Relative" "Licensed/Unlicensed Relative Home" "Out of State Parental Home" ///
	"Adoptive Home" "Out of State Unlicensed Relative" "Parental Home" "Pre-Adoptive Home" ///
	"Terminated Parental Home" {
	replace type_kin=1 if placement_type=="`x'"
}
foreach x in "Adult Foster Home" "College Dormitory" "AWOL" "EPIC Guardianship Home" ///
	"Friend/Partner Home" "Independent Living"  "Juvenile Guardianship Home" "Licensed Unrelated Foster Home" ///
	"NO MATCH FOUND" "Out of State Foster Home" "Out-of-State Private Agency" "Private Agency Foster Care" ///
	"Rental Home/Apartment" "Unrelated Caregiver" {
	replace type_unrel=1 if placement_type=="`x'"
}
foreach x in "Agency" "Boarding School, etc. (other)" "Child Caring Institution" "Community Justice Center" ///
	"Court Treatment Facility" "Detention" "Emergency Residential Shelter" "Emergency Shelter Home" ///
	"Hospital" "Jail" "MDHHS Training School" "Mental Health Facility" "Out of State Child Caring Institution" ///
	"Runaway Service Facility" {
	replace type_group_inst=1 if placement_type=="`x'"
}
drop placement_type
gen placement_type=1 if type_kin==1
replace placement_type=2 if type_unrel==1
replace placement_type=3 if type_group_inst==1

**Count the number of placements a child experienced
bysort vicid: gen n_placements=_N
bysort vicid: egen terminal_max=max(terminal)
replace n_placements=n_placements-terminal_max //the number of placements should not include reunification or adoption//

**Collapse to child level, with info from their first placement and terminal outcome
replace fc_enddt=0 if fc_enddt==.
bysort vicid placementid: egen fc_enddt1=max(fc_enddt)
drop fc_enddt
rename fc_enddt1 fc_enddt
replace fc_enddt=. if fc_enddt==0

bysort vicid: egen start_min=min(fc_startdt)
keep if startdt==start_min
drop if startdt==enddt
keep vicid placementid placement_type type* fc_startdt fc_enddt n_placements

**Topcode number of placements
sum n_placements, d
replace n_placements=r(p99) if n_placements>r(p99)

la var placementid "Placement ID"
la var fc_startdt "Should equal removal date (from living arrangements data)"
la var type_kin "First placement was kinship"
la var type_unrel "First placement was foster family"
la var type_group_inst "First placement was group home/institution"
la var placement_type "First placement type (categorical)"
la var fc_enddt "Reunification, adoption or age out date"
la var n_placements "# Different Foster Placements"

sort vicid

save "$cleandata/living_arrangement_clean.dta", replace





























